//
//  MyDetailOrderFootView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/25.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//
//  订单详情下半部分视图
import UIKit
import SwiftyJSON

protocol MyDetailOrderFootViewDelegte: NSObjectProtocol {
    /// 确认收货
    func paymentButtonTap(mode: myOrderModel)
    /// 评论
    func evaluateButtonTap(mode: myOrderModel)
    
    // 售后
    func customerServiceTap(mode: myOrderModel)
}

class MyDetailOrderFootView: UIView {
    weak var delegate: MyDetailOrderFootViewDelegte?
    weak var delegateVC: UIViewController?
    private var orderDetailType: OrderDetailType = .waitPay
    private let superTopOffset: CGFloat = 0
    private let rightMargin: CGFloat = 24
    private let leftMargin: CGFloat = 16
    private let TopMargin: CGFloat = 5
    private let bttomOffset: CGFloat = 20

    private let totalPriceplaceholderLabelTopMargin: CGFloat = 19
    
    var orderLable: UILabel?
    var timeLable: UILabel?
    var payLable: UILabel?
    var fapiaoLable: UILabel?
    var totalLable: UILabel?
    var yunfeiLable: UILabel?
    var moneyLable: UILabel?
    var copyBtn: UIButton?
    var doneBtn: UIButton?
    var leftBottomBtn: UIButton?
  
    var model: myOrderModel?{
        didSet{
            orderLable?.text = "订单编号：" +  (model?.orderNumber)!
            timeLable?.text = "下单时间：" + (model?.submitTime)!
            payLable?.text = "支付方式：" + (model?.payMethod)!
            if model?.invoiceType != "null" {
                fapiaoLable?.text = "发票类型：" + (model?.invoiceType)!
            }else {
                fapiaoLable?.text = "发票类型：不开发票"
            }
            var price: Float = 0
            for subject in (model?.purchaseGoodsInfos)! {
                let json = JSON(subject.totalPrice).floatValue
                price += json
            }
            totalLable?.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "￥：", firstColor: UIColor(hex: 0xAF3939), firstSize: 10), second: (secondString: model?.payAmount as! NSString, secondColor: UIColor(hex: 0xAF3939), secondSize: 17))
            yunfeiLable?.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "￥", firstColor: UIColor.gray, firstSize: 8), second: (secondString: model?.carriage as! NSString, secondColor: UIColor.gray, secondSize: 13))
            
            moneyLable?.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "需支付：￥", firstColor: UIColor(hex: 0xC30D23), firstSize: 10), second: (secondString: model?.actualPayAmount as! NSString, secondColor: UIColor(hex: 0xC30D23), secondSize: 16))
            doneBtn?.setTitle(model?.statusName, for: .normal)
            
            orderLable?.sizeToFit()
            timeLable?.sizeToFit()
//            payLable?.sizeToFit()
            fapiaoLable?.sizeToFit()
            totalLable?.sizeToFit()
            yunfeiLable?.sizeToFit()
            moneyLable?.sizeToFit()
            moneyLable?.right = self.width - 20
            copyBtn?.left = (orderLable?.right)! + 10
            if model?.status == "0" {
                leftBottomBtn?.setTitle("取消订单", for: .normal)
                leftBottomBtn?.setTitleColor(UIColor.gray, for: .normal)
                leftBottomBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
                doneBtn?.setTitle("付款", for: .normal)
                doneBtn?.setTitleColor(UIColor.gray, for: .normal)
                doneBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
            }else if model?.status == "1" {
                doneBtn?.setTitle("催单", for: .normal)
            }else if model?.status == "2" {
                doneBtn?.setTitle("确认收货", for: .normal)
            }else if model?.status == "3" {
                leftBottomBtn?.setTitle("申请售后", for: .normal)
                leftBottomBtn?.setTitleColor(UIColor.gray, for: .normal)
                leftBottomBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
                doneBtn?.setTitle("评价", for: .normal)
                doneBtn?.setTitleColor(UIColor.gray, for: .normal)
                doneBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
            }else if model?.status == "4" {
                leftBottomBtn?.setTitle("删除订单", for: .normal)
                leftBottomBtn?.setTitleColor(UIColor.gray, for: .normal)
                leftBottomBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
                doneBtn?.setTitle("再次购买", for: .normal)
                doneBtn?.setTitleColor(UIColor.gray, for: .normal)
                doneBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
                
            }else{
                leftBottomBtn?.setTitle("取消订单", for: .normal)
                leftBottomBtn?.setTitleColor(UIColor.gray, for: .normal)
                leftBottomBtn?.layer.borderColor = ONColor.orderNormalTextColor.cgColor
            }
        }
    }
    func showData(type: OrderDetailType) {
        orderDetailType = type
        self.backgroundColor = ONColor.cellBGColor
        self.frame = CGRect(x: 0, y: 0, width: ScreenWidth - 20, height: 120)
        let _ = subOrderDatas()
        bottomView()
//        switch type {
//        case .waitPay:
//            let _ = subOrderDatas()
//        default:
//            bottomView()
//        }
    }
    
    private func subOrderDatas() -> (mainView: UIView, invoiceLabel: UILabel) {
        let mainView = UIView()
        let textColor = UIColor(hex: 0x1A1A1A)
        mainView.backgroundColor = UIColor.white
        mainView.frame = CGRect(x: 0, y: -superTopOffset, width: self.width, height: 100)
        self.addSubview(mainView)
        
        let orderNumberLabel = UILabel()
        orderNumberLabel.text = "订单编号：77779888"
        orderNumberLabel.font = UIFont.systemFont(ofSize: 13)
        orderNumberLabel.textColor = textColor
        orderNumberLabel.sizeToFit()
        orderNumberLabel.frame.origin = CGPoint(x: leftMargin, y: 15 + superTopOffset)
        mainView.addSubview(orderNumberLabel)
        orderLable = orderNumberLabel
        
        let copyButton = UIButton(type: .custom)
        copyButton.setTitle("复制", for: .normal)
        copyButton.setTitleColor(UIColor(hex: 0x5791D3), for: .normal)
        copyButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        copyButton.addTarget(self, action: #selector(copyTap), for: .touchUpInside)
        copyButton.sizeToFit()
        copyButton.frame = CGRect(x: orderNumberLabel.right + 5, y: TopMargin, width: copyButton.width + 10, height: copyButton.height)
        copyButton.centerY = orderNumberLabel.centerY
        mainView.addSubview(copyButton)
        self.copyBtn = copyButton
        
        let timeLabel = UILabel()
        timeLabel.text = "下单时间：2018-01-12 10:36:39"
        timeLabel.font = UIFont.systemFont(ofSize: 13)
        timeLabel.textColor = textColor
        timeLabel.sizeToFit()
        timeLabel.frame.origin = CGPoint(x: leftMargin, y: orderNumberLabel.bottom + TopMargin)
        mainView.addSubview(timeLabel)
        self.timeLable = timeLabel
        
        let payTypeLabel = UILabel()
        payTypeLabel.text = "支付方式：微信支付"
        payTypeLabel.font = UIFont.systemFont(ofSize: 13)
        payTypeLabel.textColor = textColor
        payTypeLabel.sizeToFit()
        payTypeLabel.frame.origin = CGPoint(x: leftMargin, y: timeLabel.bottom + TopMargin)
        mainView.addSubview(payTypeLabel)
        payLable = payTypeLabel
        payTypeLabel.height = 0
        
        let invoiceLabel = UILabel()
        invoiceLabel.text = "发票类型：不开发票"
        invoiceLabel.font = UIFont.systemFont(ofSize: 13)
        invoiceLabel.textColor = textColor
        invoiceLabel.sizeToFit()
        invoiceLabel.frame.origin = CGPoint(x: leftMargin, y: payTypeLabel.bottom + TopMargin)
        mainView.addSubview(invoiceLabel)
        fapiaoLable = invoiceLabel
        
        if orderDetailType == .waitPay {
            let priceLabel = UILabel()
            priceLabel.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "实付款：", firstColor: UIColor(hex: 0xAF3939), firstSize: 10), second: (secondString: "1314", secondColor: UIColor(hex: 0xAF3939), secondSize: 17))
//            mainView.addSubview(priceLabel)
//            priceLabel.snp.makeConstraints({ (make) in
//                make.right.equalTo(mainView.snp.right).offset(-20)
//                make.centerY.equalTo(invoiceLabel.snp.centerY)
//            })
            moneyLable = priceLabel
            
            mainView.height = invoiceLabel.bottom + bttomOffset
            self.height = mainView.height + bttomOffset
//            setShadowImage(mainView)
        }
        return (mainView, invoiceLabel)
    }
    
    func bottomView() {
        let textColor = UIColor(hex: 0x626262)
        let items = subOrderDatas()
        let mainView = items.mainView
        let lastLabel = items.invoiceLabel
        let lineView = UIView()
        lineView.backgroundColor = ONColor.cutLineColor
        lineView.frame = CGRect(x: leftMargin, y: lastLabel.bottom + 10, width: mainView.width - leftMargin * 2, height: 1)
        mainView.addSubview(lineView)
        
        let totalPriceplaceholderLabel = UILabel()
        totalPriceplaceholderLabel.font = UIFont.systemFont(ofSize: 11)
        totalPriceplaceholderLabel.textColor = textColor
        totalPriceplaceholderLabel.text = "商品总额："
        totalPriceplaceholderLabel.sizeToFit()
        totalPriceplaceholderLabel.frame.origin = CGPoint(x: leftMargin, y: lineView.bottom
            + totalPriceplaceholderLabelTopMargin)
        mainView.addSubview(totalPriceplaceholderLabel)
        
        
        let totalPriceLabel = UILabel()
        totalPriceLabel.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "￥", firstColor: textColor, firstSize: 10), second: (secondString: "1314", secondColor: textColor, secondSize: 14))
        totalPriceLabel.sizeToFit()
        totalPriceLabel.frame.origin = CGPoint(x: totalPriceplaceholderLabel.right, y: lineView.bottom
            + totalPriceplaceholderLabelTopMargin - 2)
        
        totalPriceLabel.bottom = totalPriceplaceholderLabel.bottom
        mainView.addSubview(totalPriceLabel)
        totalLable = totalPriceLabel
        
        let freightLabel = UILabel()
        freightLabel.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "￥", firstColor: textColor, firstSize: 8), second: (secondString: "0.00", secondColor: textColor, secondSize: 13))
        freightLabel.sizeToFit()
        mainView.addSubview(freightLabel)
        yunfeiLable = freightLabel
        
        let freightLabelplaceholderLabel = UILabel()
        freightLabelplaceholderLabel.font = UIFont.systemFont(ofSize: 10)
        freightLabelplaceholderLabel.textColor = textColor
        freightLabelplaceholderLabel.text = "运费："
        freightLabelplaceholderLabel.sizeToFit()
        freightLabelplaceholderLabel.frame.origin = CGPoint(x: self.width - freightLabelplaceholderLabel.width - rightMargin - freightLabel.width, y: lineView.bottom
            + totalPriceplaceholderLabelTopMargin)
        mainView.addSubview(freightLabelplaceholderLabel)
        freightLabel.left = freightLabelplaceholderLabel.right
        freightLabel.bottom = freightLabelplaceholderLabel.bottom
        
        let realPayment = UILabel()
        realPayment.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "实付款：￥", firstColor: UIColor(hex: 0xC30D23), firstSize: 10), second: (secondString: "1314", secondColor: UIColor(hex: 0xC30D23), secondSize: 16))
        moneyLable = realPayment
        realPayment.sizeToFit()
        realPayment.frame.origin = CGPoint(x: mainView.width - realPayment.width - rightMargin, y: freightLabel.bottom + rightMargin - 20)
        mainView.addSubview(realPayment)
        
        let bottomLineView = UIView()
        bottomLineView.backgroundColor = ONColor.cutLineColor
        bottomLineView.frame = CGRect(x: leftMargin, y: realPayment.bottom + bttomOffset, width: mainView.width - leftMargin * 2, height: 1)
        mainView.addSubview(bottomLineView)
        
        let doneButton = UIButton(type: .custom)
        let doneButtonWidht: CGFloat = 80
        let doneButtonHeight: CGFloat = 30
        let doneButtonTitle = orderDetailType == .waitPay ? "确认收货" : "评价"
        doneButton.tag = orderDetailType == .waitPay ? 10 : 20
        doneButton.setTitle(doneButtonTitle, for: .normal)
        doneButton.setTitleColor(UIColor(hex: 0xC30D23), for: .normal)
        doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        doneButton.addTarget(self, action: #selector(doneTap(_ :)), for: .touchUpInside)
        doneButton.frame = CGRect(x: mainView.width - rightMargin + 5 - doneButtonWidht, y: bottomLineView.bottom + 13, width: doneButtonWidht, height: doneButtonHeight)
        doneButton.layer.cornerRadius = doneButtonHeight / 2
        doneButton.layer.borderColor = UIColor(hex: 0xC30D23).cgColor
        doneButton.layer.borderWidth = 1
        mainView.addSubview(doneButton)
        doneBtn = doneButton
        
        let customerServiceButton = UIButton(type: .custom)
            let customerServiceWidth: CGFloat = 80
            let customerServiceHeight: CGFloat = 30
            let customerServiceButtonOffset: CGFloat = 20
            customerServiceButton.setTitle("售后服务", for: .normal)
            customerServiceButton.setTitleColor(ONColor.orderNormalTextColor, for: .normal)
            customerServiceButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            customerServiceButton.addTarget(self, action: #selector(customerServiceTap(_ :)), for: .touchUpInside)
            customerServiceButton.frame = CGRect(x: doneButton.left - doneButtonWidht - customerServiceButtonOffset, y: bottomLineView.bottom + 13, width: customerServiceWidth, height: customerServiceHeight)
            customerServiceButton.layer.cornerRadius = doneButtonHeight / 2
            customerServiceButton.layer.borderColor = ONColor.orderNormalTextColor.cgColor
            customerServiceButton.layer.borderWidth = 1
            leftBottomBtn = customerServiceButton
            mainView.addSubview(customerServiceButton)
        
        mainView.height = doneButton.bottom + bttomOffset
        self.height = mainView.height + bttomOffset
        setShadowImage(mainView)
    }
    
    func setShadowImage(_ mainView: UIView) {
        let imageView = UIImageView(image: UIImage(named: "bg_cut_shadow"))
        imageView.sizeToFit()
        imageView.frame = CGRect(x: -2, y: mainView.bottom - imageView.height / 2, width: mainView.width + 4, height: imageView.height)
        mainView.addSubview(imageView)
    }
    
    // MARK: - copy
    @objc func copyTap() {
        CustomShowAlertView().show(title:
            "复制成功"
        , showTime: 0.5) {
            
        }
        let pasteboard = UIPasteboard.general
        pasteboard.string = self.model?.orderNumber
    }
    
    @objc func doneTap (_ btn: UIButton) {
     
        delegate?.paymentButtonTap(mode: model!)
       
    }
    
    @objc func customerServiceTap(_ btn: UIButton) {
        
        delegate?.customerServiceTap(mode: model!)
        
    }
    
    
}
